package com.cc.flyingfish.advice;

import com.cc.flyingfish.util.ConditionException;
import com.cc.flyingfish.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

import static com.cc.flyingfish.util.BusinessStatusConstant.BUSINESSEXCEPTION;
import static com.cc.flyingfish.util.BusinessStatusConstant.BUSINESSEXCEPTION_MESSAGE;

/**
 * @author CHENCHEN
 * @title
 * @createTime 2023-02-27 22:20
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(value = Exception.class)
    public CommonResponse handlerFlyingFishException(HttpServletRequest request, Exception e){
        log.error("ExceptionAdvice receive Exception is : [{}]", e.getMessage(), e);
        if(e instanceof ConditionException){
            Integer code = ((ConditionException)e).getCode();
            return CommonResponse.BUSINESSEXCEPTION(code, e.getMessage());
        }
        return CommonResponse.BUSINESSEXCEPTION(BUSINESSEXCEPTION, BUSINESSEXCEPTION_MESSAGE, e);
    }
}
